using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.Plugins;
using Styx.WoWInternals;
using Styx.WoWInternals.Misc;
using Styx.WoWInternals.World;
using Styx.WoWInternals.WoWCache;
using Styx.WoWInternals.WoWObjects;
using Styx.Pathing;
using Styx.Helpers;

namespace QuickPlugins
{
    public class AutoBag : HBPlugin
    {
        private static LocalPlayer Me = StyxWoW.Me;
		private static Stopwatch stoper = new Stopwatch();
		public override void Pulse()
        {
		if(!Me.Combat && Me.IsAlive)
		{
var NewBag= ObjectManager.GetObjectsOfType<WoWContainer>().Where(o => o.BagIndex!=null && o.BagSlot!=null && !(o.BagSlot==-1 && (o.BagIndex==3 || o.BagIndex==2 || o.BagIndex==1 || o.BagIndex==0 || o.BagIndex==-1))).OrderByDescending(o => o.Slots).FirstOrDefault();
var bag1=(StyxWoW.Me.GetBagAtIndex(0));
var bag2=(StyxWoW.Me.GetBagAtIndex(1));
var bag3=(StyxWoW.Me.GetBagAtIndex(2));
var bag4=(StyxWoW.Me.GetBagAtIndex(3));
//Logging.Write(Convert.ToString(bag1.Slots));
//Logging.Write(Convert.ToString(bag2.Slots));
//Logging.Write(Convert.ToString(bag3.Slots));
//Logging.Write(Convert.ToString(bag4.Slots));
if(NewBag!=null)
{
//Logging.Write(NewBag.Name);
//Logging.Write(Convert.ToString(NewBag.BagIndex));
//Logging.Write(Convert.ToString(NewBag.BagSlot));
if(NewBag.Slots>bag1.Slots || NewBag.Slots>bag2.Slots || NewBag.Slots>bag3.Slots || NewBag.Slots>bag4.Slots)
{
Logging.Write(NewBag.Name);
if(bag1!=null && bag2!=null && bag3!=null && bag4!=null)
{
if (bag1.Slots <= bag2.Slots && bag1.Slots <= bag3.Slots && bag1.Slots <= bag4.Slots)
{
int smallbag=20;
Logging.Write("First bag");
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); PickupContainerItem(0, 16); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); EquipCursorItem({1}); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
return;
}
if (bag2.Slots <= bag1.Slots && bag2.Slots <= bag3.Slots && bag2.Slots <= bag4.Slots)
{
int smallbag=21;
Logging.Write("Second bag");
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); PickupContainerItem(0, 16); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); EquipCursorItem({1}); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
return;
}
if (bag3.Slots <= bag2.Slots && bag3.Slots <= bag1.Slots && bag3.Slots <= bag4.Slots)
{
int smallbag=22;
Logging.Write("Third bag");
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); PickupContainerItem(0, 16); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); EquipCursorItem({1}); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
return;
}
if (bag4.Slots <= bag2.Slots && bag4.Slots <= bag1.Slots && bag4.Slots <= bag3.Slots)
{
int smallbag=23;
Logging.Write("Fourth bag");
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); PickupContainerItem(0, 16); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
Lua.DoString("ClearCursor(); for bag = 0,4,1 do for slot = 1, 32, 1 do local name = GetContainerItemLink(bag,slot); if name and string.find(name,'{0}') then PickupContainerItem(bag,slot); EquipCursorItem({1}); if StaticPopup1Button1 and StaticPopup1Button1:IsVisible() then StaticPopup1Button1:Click(); end; end; end; end;", NewBag.Entry, smallbag);
Thread.Sleep(1000);
return;
}

}
}
}
}
		}
        public override string Name { get { return "AutoBag"; } }

        public override string Author { get { return "Mat53"; } }

        public override Version Version { get { return new Version(1,0,0,0);} }
    }
}